﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Windows.Forms;

namespace Billing
{
    public class Manifest
    {
        #region Static Protected Methods

        static protected XmlNamespaceManager GetNamespaceManager(XmlDocument doc)
        {
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
            nsmgr.AddNamespace(string.Empty, "urn:schemas-microsoft-com:asm.v2");
            nsmgr.AddNamespace("dsig", "http://www.w3.org/2000/09/xmldsig#");
            nsmgr.AddNamespace("asmv1", "urn:schemas-microsoft-com:asm.v1");
            nsmgr.AddNamespace("asmv2", "urn:schemas-microsoft-com:asm.v2");
            nsmgr.AddNamespace("xrml", "urn:mpeg:mpeg21:2003:01-REL-R-NS");
            nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
            return nsmgr;
        }

        #endregion

        #region Private Fields

        private XmlDocument mDocument;
        private XmlNamespaceManager mNamespaceManager;
        private Dictionary<string, DependAssembly> mDependencies;
        private Dictionary<string, AssemblyFile> mAssemblyFiles;
        private AssemblyIdentity mAssemblyIdentity;

        #endregion

        #region Protected Properties

        protected XmlDocument Document
        {
            get { return mDocument; }
            set { mDocument = value; }
        }
        protected XmlNamespaceManager NamespaceManager
        {
            get { return mNamespaceManager; }
            set { mNamespaceManager = value; }
        }

        #endregion

        #region Public Properties

        public Dictionary<string, DependAssembly> Dependencies
        {
            get { return mDependencies; }
        }
        public Dictionary<string, AssemblyFile> AssemblyFiles
        {
            get { return mAssemblyFiles; }
        }
        public AssemblyIdentity AssemblyIdentity
        {
            get { return mAssemblyIdentity; }
            set { mAssemblyIdentity = value; }
        }

        #endregion

        #region Constructors

        public Manifest()
        {
            mDependencies = new Dictionary<string, DependAssembly>();
            mAssemblyFiles = new Dictionary<string, AssemblyFile>();
        }
        public Manifest(XmlDocument document)
            : this()
        {
            LoadFromUri(document);
        }
        public Manifest(string path)
            : this()
        {
            XmlDocument document = new XmlDocument();
            try
            {
                document.Load(path);
            }
            catch
            {
                MessageBox.Show(path + " ფაილი არ იქნა ნაპოვნი!");
            }
            LoadFromUri(document);
        }

        #endregion

        #region Public Methods

        public void LoadFromUri(XmlDocument document)
        {
            // Load a namespace manager for this Manifest
            Document = document;
            NamespaceManager = GetNamespaceManager(Document);

            // Load assembly identity
            XmlNode aiNode = Document.SelectSingleNode(
                "/asmv1:assembly/asmv1:assemblyIdentity",
                NamespaceManager);
            if (aiNode != null)
            {
                AssemblyIdentity = new AssemblyIdentity(aiNode, NamespaceManager);
            }

            // Load each dependency
            foreach (XmlNode node in Document.SelectNodes("/asmv1:assembly/asmv2:dependency", NamespaceManager))
            {
                foreach (XmlNode dependentAssembly in node.SelectNodes("asmv2:dependentAssembly", NamespaceManager))
                {
                    DependAssembly dependAssembly = new DependAssembly(dependentAssembly, NamespaceManager);
                    Dependencies.Add(dependAssembly.AssemblyIdentity.Name, dependAssembly);
                }
            }
            // Load assembly files
            foreach (XmlNode fileNode in Document.SelectNodes("/asmv1:assembly/asmv2:file", NamespaceManager))
            {
                AssemblyFile assemblyFile = new AssemblyFile(fileNode, NamespaceManager);
                AssemblyFiles.Add(assemblyFile.Name, assemblyFile);
            }
        }

        #endregion
    }
}
